package com.drg.center.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.drg.center.entity.DictDiagnosisHisPO;
import com.drg.center.entity.DictDiagnosisPO;
import com.drg.center.entity.param.DiagnosisGroupPageDTO;
import com.drg.center.mapper.DictDiagnosisMapper;
import com.guaike.core.mybatis.plugins.BaseDAO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class DictDiagnosisDAO extends BaseDAO<DictDiagnosisMapper, DictDiagnosisPO> {


    public Page<DictDiagnosisPO> findDiagnosisGroupPage(DiagnosisGroupPageDTO param) {
        LambdaQueryWrapper<DictDiagnosisPO> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getMdcCode())) {
            queryWrapper.eq(DictDiagnosisPO::getMdcCode, param.getMdcCode());
        }
        if (StringUtils.isNotBlank(param.getAdrgCode())) {
            queryWrapper.eq(DictDiagnosisPO::getAdrgCode, param.getAdrgCode());
        }
        if (StringUtils.isNotBlank(param.getSearch())) {
            queryWrapper.and(wq -> wq.like(DictDiagnosisPO::getDiagnosisCode, param.getSearch()).or().like(DictDiagnosisPO::getDiagnosisName, param.getSearch()));
        }
        queryWrapper.orderByDesc(DictDiagnosisPO::getId);
        return getBaseMapper().selectPage(new Page<>(param.getPageIndex(), param.getPageSize()), queryWrapper);
    }

    public List<DictDiagnosisPO> findByMdcAndMain(String mdcCode, String operationCode) {
        LambdaQueryWrapper<DictDiagnosisPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DictDiagnosisPO::getMdcCode, mdcCode);
        queryWrapper.eq(DictDiagnosisPO::getDiagnosisCode, operationCode);
        return getBaseMapper().selectList(queryWrapper);
    }

    public List<DictDiagnosisPO> searchNoMatchMedicalDiagnosis(String search, List<String> list) {
        LambdaQueryWrapper<DictDiagnosisPO> queryWrapper = new LambdaQueryWrapper<>();
        if (CollectionUtils.isNotEmpty(list)) {
            queryWrapper.notIn(DictDiagnosisPO::getDiagnosisCode, list);
            queryWrapper.and(wrapper -> wrapper.like(DictDiagnosisPO::getDiagnosisCode, search)
                    .or().like(DictDiagnosisPO::getDiagnosisName, search));
        } else {
            queryWrapper.like(DictDiagnosisPO::getDiagnosisCode, search)
                    .or().like(DictDiagnosisPO::getDiagnosisName, search);
        }
        queryWrapper.last("limit 0,20");
        return getBaseMapper().selectList(queryWrapper);
    }

}
